package com.liang.circle.server.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.liang.auth.api.entity.Result;
import com.liang.circle.api.req.ShareCommentGetReq;
import com.liang.circle.api.req.ShareCommentRemoveReq;
import com.liang.circle.api.req.ShareCommentReplySaveReq;
import com.liang.circle.api.vo.ShareCommentReplyVO;
import com.liang.circle.server.sensitive.WordFilter;
import com.liang.circle.server.service.ShareCommentReplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/circle/share/comment")
@RequiredArgsConstructor
public class ShareCommentController {

    private final ShareCommentReplyService shareCommentReplyService;
    private final WordFilter wordFilter;

    /**
     * 发布内容
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody ShareCommentReplySaveReq requestParam) {
        try {
            log.info("发布内容入参：{}", JSON.toJSONString(requestParam));

            Preconditions.checkNotNull(requestParam, "参数不能为空");
            Preconditions.checkNotNull(requestParam.getReplyType(), "回复类型不能为空");
            Preconditions.checkNotNull(requestParam.getMomentId(), "评论id不能为空");
            Preconditions.checkNotNull(requestParam.getContent(), "评论或回复内容不能为空");

            wordFilter.check(requestParam.getContent());
            shareCommentReplyService.saveComment(requestParam);
            return Result.ok("发布内容成功");
        } catch (Exception e) {
            log.error("发布内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("发布内容异常！" + e.getMessage());
        }
    }

    /**
     * 查询动态下的评论
     */
    @PostMapping("/list")
    public Result<List<ShareCommentReplyVO>> list(@RequestBody ShareCommentGetReq requestParam) {
        try {
            log.info("查询动态下的评论入参：{}", JSON.toJSONString(requestParam));

            Preconditions.checkNotNull(requestParam, "参数不能为空");
            Preconditions.checkNotNull(requestParam.getId(), "动态ID不能为空");

            List<ShareCommentReplyVO> res = shareCommentReplyService.listComment(requestParam);
            return Result.ok(res);
        } catch (Exception e) {
            log.error("查询动态下的评论异常！错误原因{}", e.getMessage(), e);
            return Result.fail("查询动态下的评论异常！");
        }
    }

    /**
     * 删除动态下的评论
     */
    @PostMapping("/remove")
    public Result<Void> remove(@RequestBody ShareCommentRemoveReq requestParam) {
        try {
            log.info("删除圈子的动态入参：{}", JSON.toJSONString(requestParam));

            Preconditions.checkNotNull(requestParam, "参数不能为空");
            Preconditions.checkNotNull(requestParam.getId(), "动态的ID不能为空");
            Preconditions.checkNotNull(requestParam.getReplyType(), "回复类型不能为空");

            shareCommentReplyService.removeComment(requestParam.getId());
            return Result.ok("删除评论成功");
        } catch (Exception e) {
            log.error("删除评论异常！错误原因{}", e.getMessage(), e);
            return Result.fail("删除评论异常！");
        }
    }
}
